package com.haoshuang.zhouzhoubang.ui.page.shop.address.add

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityAddAddressBinding
import com.haoshuang.zhouzhoubang.model.bean.AddressBean
import com.haoshuang.zhouzhoubang.ui.page.shop.address.area.ServiceAreaActivity3
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPageForResult
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *  添加地址界面
 */
class AddAddressActivity : BaseVMActivity<AddAddressViewModel, ActivityAddAddressBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply {
            val defaulrListener = View.OnClickListener {
                if (is_default == "1") {
                    is_default = "0"
                    ivSetDefault.setImageResource(R.mipmap.dz_wx)
                } else {
                    is_default = "1"
                    ivSetDefault.setImageResource(R.mipmap.dz_ok)
                }

            }
            val sirListener = View.OnClickListener {
                ivLady.setImageResource(R.mipmap.dz_wx)
                ivSir.setImageResource(R.mipmap.dz_ok)
                gender = "0"
            }

            val ladyListener = View.OnClickListener {
                ivLady.setImageResource(R.mipmap.dz_ok)
                ivSir.setImageResource(R.mipmap.dz_wx)
                gender = "1"
            }
            appBar.ivBack.clickWithTrigger { finish() }
            llAddAddress.clickWithTrigger {
                startPageForResult<ServiceAreaActivity3> { code, data ->
                    if (code == 137 && data != null) {
                        province = data.getStringExtra("city1").toString()
                        city = data.getStringExtra("city2").toString()
                        district = data.getStringExtra("city3").toString()

                        binding.tvAdr.text = province + city + district
                    }
                }
            }
            if (ids == 0) {
                appBar.tvTitle.text = "添加地址"
            } else {
                appBar.tvTitle.text = "编辑地址"
            }

            ivSetDefault.setOnClickListener(defaulrListener)
            tvSetDefault.setOnClickListener(defaulrListener)

            tvSir.setOnClickListener(sirListener)
            ivSir.setOnClickListener(sirListener)
            tvLady.setOnClickListener(ladyListener)
            ivLady.setOnClickListener(ladyListener)
            tvCommit.clickWithTrigger {
                var real_name = tvName.text.toString()
                var phone = tvPhone.text.toString()
                if (real_name.isNullOrBlank()) {
                    toast("姓名不能为空")
                    return@clickWithTrigger
                }
                if (phone.isNullOrBlank()) {
                    toast("手机号不能为空")
                    return@clickWithTrigger
                }
                if (province.isNullOrBlank()) {
                    toast("请选择地址")
                    return@clickWithTrigger
                }
                var detail = tvDeail.text.toString()
                if (detail.isEmpty()){
                    toast("请输入详细地址")
                    return@clickWithTrigger
                }
                if (ids != 0) {
                    vm.getData(
                        "real_name" to real_name,
                        "province" to province,
                        "id" to ids,
                        "city" to city,
                        "detail" to detail,
                        "district" to district,
                        "is_default" to is_default,
                        "gender" to gender,
                        "phone" to phone
                    )
                } else {
                    vm.getData(
                        "real_name" to real_name,
                        "province" to province,
                        "city" to city,
                        "detail" to detail,
                        "district" to district,
                        "is_default" to is_default,
                        "gender" to gender,
                        "phone" to phone
                    )
                }

            }

        }
    }

    var is_default = "0"
    var gender = "1"
    var ids = 0
    var province = ""
    var city = ""

    var district = ""
    override fun initData() {
        ids = intent.getIntExtra("ids", 0)
        if (ids != 0) {
            var bean = intent.getSerializableExtra("bean") as AddressBean
            province = bean.province
            city = bean.city
            district = bean.district
            binding.tvDeail.setText(bean.detail)
            binding.tvPhone.setText(bean.phone)
            binding.tvAdr.text = province + city + district
            if (is_default == "0") {
                is_default == "0"
                binding.ivSetDefault.setImageResource(R.mipmap.dz_wx)
            } else {
                is_default == "1"
                binding.ivSetDefault.setImageResource(R.mipmap.dz_ok)
            }
        }
    }

    override fun startObserve() {
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("提交成功")
                finish()
            }
        }
    }

}